

#import "MailContentViewController.h"
#import "TextViewCell.h"
#import "IDominoAppDelegate.h"


@implementation MailContentViewController


@synthesize myTableView, fromAddress, toAddress, toolbar, textView, subjectView;

- (id)init
{
	self = [super init];
	if (self)
	{
		CGRect mainViewBounds = self.view.bounds;
		toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
															  CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - 90,
															  CGRectGetWidth(mainViewBounds),
															  50)];
		
		myTableView = [[UITableView alloc] initWithFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
																   CGRectGetMinY(mainViewBounds) ,
																   CGRectGetWidth(mainViewBounds),
																   CGRectGetHeight(mainViewBounds)-50+4)
												  style:UITableViewStylePlain];
		myTableView.delegate = self;
		myTableView.dataSource = self;
		//myTableView.scrollEnabled=NO;
		myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
		myTableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
		[self.view addSubview:myTableView];	
		[self.view addSubview:toolbar];
		self.hidesBottomBarWhenPushed=YES;
		
		IDominoAppDelegate *appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];

		
		UIBarButtonItem *webViewItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:	UIBarButtonSystemItemSearch
																					 target:appDelegate action:@selector(webViewAction:)];
		
		UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
																				  target:nil action:nil];
		UIBarButtonItem *forwardItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:	UIBarButtonSystemItemAction
																					 target:appDelegate action:@selector(forwardAction:)];
		UIBarButtonItem *trashItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:	UIBarButtonSystemItemTrash
																				   target:appDelegate action:@selector(trashAction:)];
		UIBarButtonItem *replyItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:	UIBarButtonSystemItemReply
																				   target:appDelegate action:@selector(replyAction:)];
		UIBarButtonItem *composeItemList = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:	UIBarButtonSystemItemCompose
																						 target:appDelegate action:@selector(composeAction:)];
		toolbar.items = [NSArray arrayWithObjects: webViewItem, flexItem, forwardItem,flexItem, trashItem,flexItem,
												 replyItem, flexItem, composeItemList, nil];
		
		[webViewItem release];
		
		[composeItemList release];
		[flexItem release];
		[forwardItem release];
		[trashItem release];
		[replyItem release];
		_mailDateOutputFormatter=[[NSDateFormatter alloc] init];
		[_mailDateOutputFormatter setDateFormat:@"dd MMMM yyyy HH:mm"];
		
	}
	
	return self;
}

- (void)dealloc
{
	[textView release];
	[subjectView release];
	[myTableView release];
	[toolbar release];
	[_mailDateOutputFormatter release];
	[super dealloc];
}

#pragma mark - UITableView delegates



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 4;
}

// to determine specific row height for each cell, override this.  In this example, each row is determined
// buy the its subviews that are embedded.
//
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	CGFloat result;
	
	if([indexPath row]==3){
		result=contentHeight;
	}
	else if([indexPath row]==2)
		result=subjectHeight;
	else
		result=30.0;
	return result;
}

// utility routine leveraged by 'cellForRowAtIndexPath' to determine which UITableViewCell to be used on a given row.
//
- (UITableViewCell *)obtainTableCellForRow:(NSInteger)row
{
	UITableViewCell *cell = nil;
	
	if ( row<2 )
		cell = [myTableView dequeueReusableCellWithIdentifier:@"labelCell"];
	else if(row==3)
		cell = [myTableView dequeueReusableCellWithIdentifier:@"textViewCell"];
	else
		;
	if (cell == nil)
	{
		if ( row<2 )
			cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"labelCell"] autorelease];
		else 
			cell = [[[TextViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"textViewCell"] autorelease];
	}
	cell.selectionStyle=UITableViewCellSelectionStyleNone;
	return cell;
}

// to determine which UITableViewCell to be used on a given row.
//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

	NSInteger row = [indexPath row];
	UITableViewCell *sourceCell = [self obtainTableCellForRow:row];
	if( row == 0 )
	{
		sourceCell.textLabel.text = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"from", @"From :"), fromAddress];
		sourceCell.textLabel.font=[UIFont systemFontOfSize:14];
	}
	else if (row == 1)
	{
		sourceCell.textLabel.text = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"To", @"To :"), toAddress];
		sourceCell.textLabel.font=[UIFont systemFontOfSize:14];
	}
	else if (row == 2)
	{
		((TextViewCell *)sourceCell).view = subjectView;
	}
	else
	{
		((TextViewCell *)sourceCell).view = textView;
		
	}
	
	return sourceCell;
}

- (UIToolbar*)toolbar
{
	return toolbar;
}



- (void)setReceiver:(NSString*)newReceiver fullSender:(NSString*)newSender subject:(NSString*)newSubject date:(NSDate*)newDate content:(NSString*)newContent
{
	self.toAddress=newReceiver;
	self.fromAddress=newSender;

	CGRect frame = CGRectMake(0.0, 0.0, 320.0, 300);
	[textView release];
	textView = [[UITextView alloc] initWithFrame:frame]  ;
	textView.font=[UIFont systemFontOfSize:14];
	textView.autoresizingMask=UIViewAutoresizingFlexibleHeight;
	textView.editable=NO;
	textView.scrollEnabled=NO;
	[myTableView becomeFirstResponder];
	[textView resignFirstResponder];

	[textView setText:newContent];

	
	frame = CGRectMake(0.0, 0.0, 60.0, 300);
	[subjectView release];
	subjectView = [[UITextView alloc] initWithFrame:frame]  ;
	subjectView.font=[UIFont systemFontOfSize:14];
	subjectView.userInteractionEnabled=NO;
	while([newSubject hasSuffix:@"\n"]){
		newSubject=[newSubject substringToIndex:[newSubject length]-1];
	}

	NSString* date=[_mailDateOutputFormatter stringFromDate:newDate];
	if(newSubject==nil)
		newSubject=@"";
	if(date==nil)
		date=@"";
	NSString* subjectAndDate=[NSString stringWithFormat:@"%@\n%@",newSubject,date];

	[subjectView setText:subjectAndDate];
	
	subjectHeight=([self determineLine:newSubject]+1)*18+12;
	contentHeight=[self determineLine:newContent]*18+12;
	if(contentHeight<(310-subjectHeight)){
		contentHeight=310-subjectHeight;
	}
	[myTableView scrollToRowAtIndexPath:[myTableView indexPathForRowAtPoint:CGPointMake(0,0)]	atScrollPosition:UITableViewScrollPositionNone animated:NO];
	[myTableView reloadData];


}

- (int)determineLine:(NSString*)string
{
	CGSize textSize=[string sizeWithFont:[UIFont systemFontOfSize:14] ];
	int lines=(textSize.width/305.00 +1);
	NSArray* words=[string componentsSeparatedByString:@" "];
	int lastLength=-10000;
	for(NSString* word in words){
		int length=[word sizeWithFont:[UIFont systemFontOfSize:14] ].width;
		if(((length+lastLength)>305) )
			lines++;
		lastLength=length;
	}
	return lines;
}



@end

